//
//  SYNavigationView.m
//  Foodie
//
//  Created by yunqi on 16/3/10.
//  Copyright © 2016年 SY. All rights reserved.
//

#import "SYNavigationItem.h"
#define SY_NAVBAR_TITLE_FONT UIFontWithSize(18) //标题
#define SY_NAVBAR_ITEM_FONT UIFontWithSize(16) //左右两边title
#define SY_NAVBAR_ITEM_DEFAULT_COLOR XColor(288, 81, 81)//左右item.title 默认颜色
#define SY_NAVBAR_ITEM_HIGHLIGHTED_COLOR XColor(198, 53, 53)//左右item.title 选中颜色
#define SY_NAVBAR_LINE_COLOR  [UIColor redColor] //线颜色
@interface SYNavigationItem()
{
    UILabel *titleVIew;
}
@end
@implementation SYNavigationItem
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
-(UILabel *)titleview
{
    if (titleVIew==nil) {
        titleVIew=[[UILabel alloc]init];
        titleVIew.backgroundColor=[UIColor clearColor];
        titleVIew.textColor=[UIColor blackColor];
        titleVIew.textAlignment=NSTextAlignmentCenter;
        titleVIew.frame = CGRectMake(0, 0, 160, 30);
        titleVIew.font=SY_NAVBAR_TITLE_FONT;
    }
    return titleVIew;
}
-(void)setShowController:(UIViewController *)showController
{
    _showController=showController;
    [self resetsubVaules];
}

-(UIView *)navBgView
{
    return _backView;
}

-(void)resetsubVaules
{
    UIView *bgView=[self navBgView];
    if (!bgView){
        bgView = [[UIView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, kNavBarDefaultHeight)];
        bgView.tag=105555;
        [self.showController.view addSubview:bgView];
        _lineView=[[UIView alloc]init];
        _lineView.frame=CGRectMake(0, kNavBarDefaultHeight-0.5, SCREEN_WIDTH, 0.5);
        _lineView.backgroundColor = SY_NAVBAR_LINE_COLOR;
        [bgView addSubview:_lineView];
        //        [self.showController.view addSubview:bgView];
        
        //        [self.showController.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
        //        bgView=self.showController.navigationController.navigationBar.overlay;
        
    }
    self.backView=bgView;
}
- (void)addSubviewWithSuperView:(UIView *)superView subView:(UIView *)subView
{
    if (subView == nil) return;
    if (![subView isDescendantOfView:superView]) {
        [superView addSubview:subView];
    }
}

- (void)layoutNavigationView
{
    
    CGFloat marginX = 15;
    if (_leftItemView != nil) {
        
        CGFloat marginY = (CGRectGetHeight(self.backView.frame) - 20 - CGRectGetHeight(_leftItemView.frame)) / 2;
        _leftItemView.frame = CGRectMake(marginX,
                                         20 + marginY,
                                         CGRectGetWidth(_leftItemView.frame),
                                         MIN(CGRectGetHeight(self.backView.frame) - 20, CGRectGetHeight(_leftItemView.frame)));
    }
    
    if (_rigthItemView != nil) {
        //        CGFloat marginRightX = _leftNavigationItemView != nil ? (APP_CONTENT_WIDTH - CGRectGetMaxX(_leftNavigationItemView.frame)) : (APP_CONTENT_WIDTH - 10 - CGRectGetWidth(_rightNavigationItemView.frame));
        
        CGFloat marginRightX =  SCREEN_WIDTH - CGRectGetWidth(_rigthItemView.frame) - marginX;
        CGFloat marginRightY = (CGRectGetHeight(self.backView.frame) - 20 - CGRectGetHeight(_rigthItemView.frame)) / 2;
        CGFloat heightRightY = MIN(CGRectGetHeight(self.backView.frame) - 20, CGRectGetHeight(_rigthItemView.frame));
        _rigthItemView.frame = CGRectMake(marginRightX,
                                          20 + marginRightY,
                                          CGRectGetWidth(_rigthItemView.frame),
                                          heightRightY);
    }
    
    if (_centerItemView != nil) {
        CGFloat margin=0;
        CGFloat marginCX=_leftItemView?(_leftItemView.x+_leftItemView.width+margin):marginX;
        CGFloat marginCRX=_rigthItemView?(_rigthItemView.x-margin):(self.backView.width-margin);
        CGFloat centerWidth=MIN(marginCRX-marginCX, _centerItemView.width);
        _centerItemView.frame=CGRectMake(marginCX, 0, centerWidth, _centerItemView.height);
        _centerItemView.centerY=20+(self.backView.height-20)/2;
        if (marginCX<(SCREEN_WIDTH-_centerItemView.width)/2) {
            _centerItemView.centerX=self.backView.width/2;
        }
    }
}


-(void)createNavLeftBtn
{
    if (!self.navigationLeftBtn) {
        self.navigationLeftBtn=[[SYNavigationitemLeftView alloc]init];
        self.navigationLeftBtn.goBackItem.titleLabel.font=SY_NAVBAR_ITEM_FONT;
        self.navigationLeftBtn.backgroundColor=[UIColor clearColor];
        [self.navigationLeftBtn.goBackItem setTitleColor:XColor(51, 51, 51) forState:UIControlStateNormal];
        self.navigationLeftBtn.hidden=YES;
    }
    [self.navigationLeftBtn resetDefault];
}

-(void)createNavRigthBtn
{
    if (!self.navigationRightBtn) {
        self.navigationRightBtn=[[SYNavigationItemBtnView alloc]init];
        self.navigationRightBtn.backgroundColor=[UIColor clearColor];
        self.navigationRightBtn.titleLabel.font=SY_NAVBAR_ITEM_FONT;
        [self.navigationRightBtn setTitleColor:XColor(51, 51, 51) forState:UIControlStateNormal];
//        [self.navigationRightBtn setTitleColor:FNColor(198, 53, 53)  forState:UIControlStateHighlighted];
        self.navigationRightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
        self.navigationRightBtn.hidden=YES;
    }
//    [self.navigationRightBtn setBackgroundImage:nil forState:UIControlStateNormal];
//    [self.navigationRightBtn setImage:nil forState:UIControlStateNormal];
//    [self.navigationRightBtn setImage:nil forState:UIControlStateHighlighted];
//    [self.navigationRightBtn setBackgroundImage:nil forState:UIControlStateHighlighted];
//    self.navigationRightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;

}
-(void)setLeftItemView:(UIView *)leftItemView
{
    _leftItemView=leftItemView;
    [self addSubviewWithSuperView:_backView subView:_leftItemView];
    [self layoutNavigationView];
}
-(void)setRigthItemView:(UIView *)rigthItemView
{
    _rigthItemView=rigthItemView;
    [self addSubviewWithSuperView:_backView subView:_rigthItemView];
    [self layoutNavigationView];

}

-(void)setCenterItemView:(UIView *)centerItemView
{
    _centerItemView=centerItemView;
    [self addSubviewWithSuperView:_backView subView:_centerItemView];
    [self layoutNavigationView];
}
-(void)setTitle:(NSString *)title
{
    if (self.centerItemView&&![self.centerItemView isEqual:[self titleview]]) {
        return;
    }
    self.centerItemView=[self titleview];
    titleVIew.text=title;
    //
}

-(void)showLeftButtonWithNomoralImage:(NSString *)strNomoralImage HighlightedImage:(NSString *)HighlightedImage title:(NSString *)title
{
    [self createNavLeftBtn];
    self.navigationLeftBtn.hidden=NO;
    if (!MSStringisEmpty(HighlightedImage)) {
        [self.navigationLeftBtn.goBackItem setImage:[UIImage imageNamed:HighlightedImage] forState:UIControlStateHighlighted];
    }
    [self.navigationLeftBtn showBackBtn:title imageStr:strNomoralImage];
    self.leftItemView = self.navigationLeftBtn;
}
-(void)showRightButtonWithNomoralImage:(NSString *)strNomoralImage HighlightedImage:(NSString *)HighlightedImage title:(NSString *)title
{
    [self createNavRigthBtn];
    self.navigationRightBtn.hidden=NO;
    if (!MSStringisEmpty(strNomoralImage)) {
        [self.navigationRightBtn setImage:[UIImage imageNamed:strNomoralImage] forState:UIControlStateNormal];
    }
    if (!MSStringisEmpty(HighlightedImage)) {
        [self.navigationRightBtn setImage:[UIImage imageNamed:HighlightedImage] forState:UIControlStateHighlighted];
    }
    CGSize imageSize=self.navigationRightBtn.currentImage.size;
    self.navigationRightBtn.frame=CGRectMake(0, 0, imageSize.width+10, 44);
    if (!MSStringisEmpty(title)) {
        self.navigationRightBtn.frame = CGRectMake(0, 0, 165/2, 44);
        [self.navigationRightBtn setTitle:title forState:UIControlStateNormal];
        self.navigationRightBtn.backgroundColor=[UIColor clearColor];
        [self.navigationRightBtn.titleLabel sizeToFit];
        self.navigationRightBtn.frame=CGRectMake(0, 0, self.navigationRightBtn.titleLabel.frame.size.width, self.navigationRightBtn.titleLabel.frame.size.height);
    }
    self.rigthItemView=self.navigationRightBtn;
}
-(void)showBackBtn:(NSString *)title imageStr:(NSString *)strImage
{
    [self createNavLeftBtn];
    self.navigationLeftBtn.hidden=NO;
    [self.navigationLeftBtn showBackBtn:title imageStr:strImage];
    self.leftItemView = self.navigationLeftBtn;
}
-(void)adjustContent
{
    CGSize size=self.navigationLeftBtn.size;
    if (self.navigationLeftBtn.goRootItem.hidden) {
        self.navigationLeftBtn.width=self.navigationLeftBtn.goBackItem.width;
    }else
    {
        self.navigationLeftBtn.width=self.navigationLeftBtn.goBackItem.width+self.navigationLeftBtn.goRootItem.width;
    }
    if (!CGSizeEqualToSize(size, self.navigationLeftBtn.frame.size)) {
        [self layoutNavigationView];
    }
}
-(void)dealloc
{
    NSLog(@"%s",__FUNCTION__);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
